package com.ljxt.dt.web.servlet.purchasingmanagementservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.HandlerInfo;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseDetailService;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseInfoService;
import com.ljxt.dt.service.purchasingmanagementservice.impl.PurchaseDetailServiceImpl;
import com.ljxt.dt.service.purchasingmanagementservice.impl.PurchaseInfoServiceImpl;
import com.ljxt.dt.util.xhq.StringUtil;
import com.ljxt.dt.vo.JsonResult;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

/**
 * @author 徐浩钦
 * @version 1.0.0
 * @date 2020/5/20 20:48
 * @copyright 老九学堂T11
 */
@WebServlet("/xhqCgddDelete")
public class CgddDeleteServlet extends HttpServlet {
    private static PurchaseInfoService purchaseInfoService = new PurchaseInfoServiceImpl();
    private static PurchaseDetailService purchaseDetailService = new PurchaseDetailServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JsonResult jsonResult = new JsonResult();

        System.out.println("CgddDeleteServlet -> doGet");
        // 获取到 orderNo 采购订单的值
        String orderNo = req.getParameter("orderNo");
        String type = req.getParameter("type");

        System.out.println("type = "+type);



        type = StringUtil.isNullAndEmpty(type) ? "" : type;

        orderNo = orderNo.trim();
        String[] strings = orderNo.split(",");

        String errorInfo = "";
        for (String string : strings) {
            boolean isDelete = purchaseInfoService.isStateIdDelete(string);
            System.out.println(string + " = " + isDelete);
            if(!isDelete){
                errorInfo += "【"+string+"】";
            }
        }

        if(errorInfo.length() > 0 ){
            System.out.println("只能删除待提交状态的订单："+errorInfo);
            jsonResult.setCode("500");

            if("delete".equals(type)){
                jsonResult.setMessage("只能删除待提交状态的订单");
            }else{
                jsonResult.setMessage("一键提交只支持待审核状态的订单");
            }

            jsonResult.setData(errorInfo);
            resp.getWriter().write(new ObjectMapper().writeValueAsString(jsonResult));
            return;
        }


        int i = 0;

        if("delete".equals(type)){
            for (String string : strings) {
                // 判断是否存在 ， 号，存在则将 ，截取掉
                int index  = string.indexOf(",");
                if(index > -1){
                    string = string.substring(index,index+1);
                    System.out.println(string);
                }



                i = purchaseInfoService.deletePurchaseInfoAndPurchaseDetail(string);
            }
            System.out.println("删除的影响行数："+i);

        }else{

            for (String string : strings) {
                System.out.println("string = "+string );
                // 判断是否存在 ， 号，存在则将 ，截取掉
                int index  = string.indexOf(",");
                if(index > -1){
                    string = string.substring(index,index+1);
                    System.out.println(string);
                }

                HandlerInfo handlerInfo = purchaseInfoService.selectObjByCgdd(string);
                // 如果没有改审核流程则 创建一个新的审核流程 为 1 ， 1 的
                if(handlerInfo == null){
                    handlerInfo = new HandlerInfo();
                    handlerInfo.setOrderNo(string);
                    handlerInfo.setName("徐浩强");
                    handlerInfo.sethandlerTypeId(1);
                    handlerInfo.setResultId(1);
                    handlerInfo.setTime(new Date());
                    handlerInfo.setRemarks("一键提交的订单");
                }
                i += purchaseInfoService.insertIntoHandlerInfo(handlerInfo);
            }
            System.out.println("提交的影响行数："+i);

        }

        if(i>-1){
            jsonResult.setCode("200");
        }else{
            jsonResult.setCode("500");
            jsonResult.setMessage("发生没有预测的错误");
            jsonResult.setData("ERROR");

        }

        resp.getWriter().write(new ObjectMapper().writeValueAsString(jsonResult));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
